using System;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;

using YourCompany.DNNBookmark.Components;
using System.Text;

namespace YourCompany.Modules.DNNBookmark
{
    public partial class Settings : ModuleSettingsBase
    {

        /// <summary>
        /// handles the loading of the module setting for this
        /// control
        /// </summary>
        public override void LoadSettings()
        {
            try
            {
                if (!IsPostBack)
                {
                    DNNBookmarkSettings settingsData = new DNNBookmarkSettings(this.TabModuleId);

                    if (settingsData.Template != null)
                    {                        
                        //txtTemplate.Text = settingsData.Template;
                        for (int i = 0; i < cblSocialBookmarks.Items.Count; i++)
                        {
                            var socialName = cblSocialBookmarks.Items[i].Text;
                            if (settingsData.Template.ToLower().Contains(socialName.ToLower()))
                            {
                                cblSocialBookmarks.Items[i].Selected = true;
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        /// <summary>
        /// handles updating the module settings for this control
        /// </summary>
        public override void UpdateSettings()
        {
            try
            {
                DNNBookmarkSettings settingsData = new DNNBookmarkSettings(this.TabModuleId);

                StringBuilder sb = new StringBuilder();
                sb.Append(@"<div class='addthis_toolbox addthis_default_style'>");
                //sb.Append(@"<a href='http://www.addthis.com/bookmark.php?v=250&amp;username=xa-4d06357a774c929a' class='addthis_button_compact'></a>");

                for (int i = 0; i < cblSocialBookmarks.Items.Count; i++)
                {
                    if (cblSocialBookmarks.Items[i].Selected)
                    {
                        sb.Append(@"<a class='addthis_button_" + cblSocialBookmarks.Items[i].Text.ToLower() + "'></a>");
                    }
                }

                sb.Append("</div>");
                sb.Append("<script type='text/javascript' src='http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d06357a774c929a'></script>");

                //settingsData.Template = txtTemplate.Text;
                settingsData.Template = sb.ToString();
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }
    }
}